package at.fhooe.im440.pandora.core;


import java.awt.event.KeyEvent;

import at.fhooe.im440.pandora.collision.CollisionTester;
import at.fhooe.im440.pandora.entities.Entity;
import at.fhooe.im440.pandora.entities.EntityManager;
import at.fhooe.im440.pandora.enumeration.Enumerations;
import at.fhooe.im440.pandora.events.EventManager;
import at.fhooe.im440.pandora.events.GameEvent;
import at.fhooe.im440.pandora.events.KeyPressedEvent;
import at.fhooe.im440.pandora.events.KeyPressedListener;
import at.fhooe.im440.pandora.graphics.GraphicsConfig;
import at.fhooe.im440.pandora.graphics.GraphicsSystem;
import at.fhooe.im440.pandora.hud.HUD;
import at.fhooe.im440.pandora.hud.HUDManager;
import at.fhooe.im440.pandora.io.InputManager;
import at.fhooe.im440.pandora.update.UpdateManager;

public class Core implements KeyPressedListener {

	public InputManager inputManager;
	public EntityManager entityManager;
	public EventManager eventManager;
	public HUDManager hudManager;
	public GraphicsSystem graphicsSystem;
	public GraphicsConfig graphicsConfig;
	public CollisionTester collisionTester;
	public UpdateManager updateManager;
	
	private float deltaTime;
	private boolean runLoop = true;
	private boolean pause = false;
	
	public boolean isDebug = true;
	
	public Core(GraphicsConfig graphicsConfig) {
		entityManager = new EntityManager(this);
		inputManager = new InputManager(this);
		eventManager = new EventManager();
		graphicsSystem = new GraphicsSystem(this);
		updateManager = new UpdateManager();
		collisionTester = new CollisionTester(this);
		this.graphicsConfig = graphicsConfig;
		setGraphicsConfiguration(graphicsConfig);
		hudManager = new HUDManager(this);
		graphicsSystem.addInputManager(inputManager);
		setUp();
	}
	
	private void setGraphicsConfiguration(GraphicsConfig gc) {
		graphicsSystem.setFrameRate(gc.FPS);
		graphicsSystem.open(gc.Width, gc.Height, gc.Fullscreen);
	}
	
	public boolean isPaused() 
	{
		return pause;
	}
	
	public void addHUD(HUD h){
		hudManager.addHUD(h);
	}
	
	public void addEntity(Entity e){
		entityManager.addEntity(e);
	}
	
	public void closeWindow(){
		graphicsSystem.close();
	}

	public float getDeltaTime() {
		return deltaTime;
	}

	public void setDeltaTime(float deltaTime) {
		this.deltaTime = deltaTime;
	}
	
	public void requestUpdate() {
		eventManager.handleEvents();
		graphicsSystem.render();
	}
	
	public boolean getRunLoop()
	{
		return runLoop;
	}
	
	public GraphicsConfig getGraphicsConfig(){
		return graphicsConfig;
	}

	@Override
	public String getType() {
		
		return null;
	}

	@Override
	public void setUp() {
		eventManager.addListener(this, "KeyPressed");
	}

	@Override
	public void tearDown() {
		eventManager.removeListener(this);
		closeWindow();
	}

	@Override
	public void onKeyPressed(KeyPressedEvent event) {
		if(event.getKeyCode() == KeyEvent.VK_ESCAPE)
		{
			//System.out.println("Beenden");
			runLoop = false;
		}
		if(event.getKeyCode() == KeyEvent.VK_P)
		{
			if(pause)
			{
				pause = false;
				eventManager.addEvent(new GameEvent(Enumerations.GameEventEnum.PAUSEEND));
			}
			else
			{
				pause = true;
				eventManager.addEvent(new GameEvent(Enumerations.GameEventEnum.PAUSESTART));
			}
		}
	}


	

}
